using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.Styling;
using FluentAvalonia.Styling;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Media;
using FluentAvalonia.UI.Navigation;
using FluentAvalonia.UI.Windowing;
using iOSClub.Toolkit.WindowApp.ViewModels.Pages;

namespace iOSClub.Toolkit.WindowApp.Views;

public partial class MainWindow : AppWindow
{
    private Dictionary<string, PageModelBase> Stack { get; set; } = new()
    {
        { "Home", new HomePageModel() },
        { "Document", new DocumentPageModel() },
        { "Setting", new SettingPageModel() },
        { "Help", new HelpPageModel() },
        { "iOS", new iOSManagerPageModel() },
        { "Plugin", new PluginPageModel() }
    };


    private void ItemInvoked(object? sender, NavigationViewItemInvokedEventArgs e)
    {
        if (e.InvokedItemContainer.Tag is not string s) return;
        if (string.IsNullOrEmpty(s)) return;
        Navigate(s);
    }

    public void Navigate(string page)
    {
        Navigate(Stack[page]);
    }

    public void Navigate(object context)
    {
        FrameView.NavigateFromObject(context, new FrameNavigationOptions());
    }

    private void OnBackRequested(object? sender, NavigationViewBackRequestedEventArgs e)
    {
        FrameView.GoBack();
    }

    private void BackClick(object? sender, RoutedEventArgs e)
    {
        FrameView.GoBack();
    }

    public MainWindow()
    {
        InitializeComponent();
        TitleBar.ExtendsContentIntoTitleBar = true;
        TitleBar.TitleBarHitTestType = TitleBarHitTestType.Complex;

        Application.Current!.ActualThemeVariantChanged += OnActualThemeVariantChanged;

        FrameView.IsNavigationStackEnabled = true;
        FrameView.NavigationPageFactory = new NavigationFactory();
        Navigate("Home");
    }

    protected override void OnOpened(EventArgs e)
    {
        base.OnOpened(e);

        var thm = ActualThemeVariant;
        if (IsWindows11 && thm != FluentAvaloniaTheme.HighContrastTheme)
        {
            TryEnableMicaEffect();
        }
    }

    private void OnActualThemeVariantChanged(object? sender, EventArgs e)
    {
        if (!IsWindows11) return;
        if (ActualThemeVariant != FluentAvaloniaTheme.HighContrastTheme)
        {
            TryEnableMicaEffect();
        }
        else
        {
            ClearValue(BackgroundProperty);
            ClearValue(TransparencyBackgroundFallbackProperty);
        }
    }

    /// <summary>
    /// 透明化（云母效果）
    /// </summary>
    private void TryEnableMicaEffect()
    {
        //return;
        TransparencyBackgroundFallback = Brushes.Transparent;
        TransparencyLevelHint = new[] { WindowTransparencyLevel.Mica };

        if (ActualThemeVariant == ThemeVariant.Dark)
        {
            var color = this.TryFindResource("SolidBackgroundFillColorBase",
                ThemeVariant.Dark, out var value)
                ? (Color2)(Color)(value ?? new Color())
                : new Color2(32, 32, 32);

            color = color.LightenPercent(-0.8f);

            Background = new ImmutableSolidColorBrush(color, 0.9);
        }
        else if (ActualThemeVariant == ThemeVariant.Light)
        {
            var color = this.TryFindResource("SolidBackgroundFillColorBase",
                ThemeVariant.Light, out var value)
                ? (Color2)(Color)(value ?? new Color())
                : new Color2(243, 243, 243);

            color = color.LightenPercent(0.5f);

            Background = new ImmutableSolidColorBrush(color, 0.9);
        }
    }

    public async void ShowControl(Control control)
    {
        var show = new ShowWindow(control);
        await show.ShowDialog(this);
    }
}